package com.cjb.usercenter.web;

import com.cjb.usercenter.entity.UserDO;
import com.cjb.usercenter.service.UserDaoService;
import com.cjb.usercenter.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Supplier;

@RestController
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserDaoService userDaoService;

    private Supplier<ResultVo> resultVoSupplier=ResultVo::new;


    @RequestMapping("/saveUser")
    @ResponseBody
    public ResultVo saveUser(@RequestBody UserDO userDO){
        ResultVo<Object> ret = new ResultVo<>();
        try{
             userDaoService.saveUser(userDO);
             ret.setCode("200");
             ret.setMessage("success");
            return ret;
        }catch(Exception exception){
            ret.setCode("500");
            ret.setMessage("error");
             logger.error("error: "+exception);
             return ret;
         }
    }





    @RequestMapping("/listUsers")
    @ResponseBody
    public ResultVo listUsers(){
        ResultVo ret = resultVoSupplier.get();
        try{
        List<UserDO> userDOList=userDaoService.listUsers();
        ret.setCode("200");
        ret.setMessage("success");
        ret.setT(userDOList);
         }catch(Exception exception){
            ret.setCode("500");
            ret.setMessage("error");
         }
        return ret;
    }




}
